
package com.hlkj.pay.controller.merchant;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.core.bean.BeanUtil;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.merchant.IMerchantConfigAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.merchant.MerchantLimitPaymentDto;
import com.hlkj.pay.dto.merchant.config.*;
import com.hlkj.pay.infrastructure.model.merchant.MerchantPaymentAccountConfigDO;
import com.hlkj.pay.service.merchant.IMerchantConfigService;
import com.hlkj.pay.vo.common.req.DeleteCodeReq;
import com.hlkj.pay.vo.merchant.req.config.*;
import com.hlkj.pay.vo.merchant.req.report.MerchantPaymentConfigMappingReq;
import com.hlkj.pay.vo.merchant.resp.report.MerchantReportPaymentConfigResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author HlpayTeam
 * @date 2024/09/12 11:32
 */
@Tag(name = "商户功能配置")
@RestController
@RequestMapping("/v1/merchant/config")
@RequiredArgsConstructor
public class MerchantConfigController extends BaseController {

    private final IMerchantConfigAppService merchantConfigAppService;

    private final IMerchantConfigService merchantConfigService;

    @Operation(summary = "保存代付配置")
    @PostMapping("/payment")
    CommonResult<Void> paymentConfig(@Validated @RequestBody MerchantPaymentConfigReq merchantPaymentConfigReq) {
        return merchantConfigAppService.paymentConfig(merchantPaymentConfigReq);
    }

    @Operation(summary = "查询代付配置")
    @GetMapping("/payment")
    CommonResult<MerchantPaymentConfigDto> queryPaymentConfig(@RequestParam(value = "sn") String sn) {
        return CommonResult.success(merchantConfigAppService.queryPaymentConfig(sn));
    }

    @Operation(summary = "保存汇电通配置")
    @PostMapping("/report")
    CommonResult<Void> reportConfig(@Validated @RequestBody MerchantReportConfigReq merchantReportConfigReq) {
        return merchantConfigAppService.reportConfig(merchantReportConfigReq);
    }

    @Operation(summary = "查询汇电通配置")
    @GetMapping("/report")
    CommonResult<MerchantReportConfigDto> queryReportConfig(@RequestParam(value = "sn") String sn) {
        return CommonResult.success(merchantConfigAppService.queryReportConfig(sn));
    }

    @Operation(summary = "汇电通白名单配置分页查询")
    @GetMapping("/payment/account/page")
    CommonResult<PageResult<MerchantReportPaymentConfigResp>> merchantPaymentConfigPage(MerchantPaymentAccountConfigPageReq accountConfigPageReq) {
        MerchantPaymentAccountConfigPageQueryDto accountConfigPageQueryDto = new MerchantPaymentAccountConfigPageQueryDto();
        accountConfigPageQueryDto.setLikeName(accountConfigPageReq.getName());
        accountConfigPageQueryDto.setLikeCardNo(accountConfigPageReq.getCardNo());
        accountConfigPageQueryDto.setAccountType(accountConfigPageReq.getAccountType());
        accountConfigPageQueryDto.setEntryType(accountConfigPageReq.getEntryType());
        accountConfigPageQueryDto.setPayeeType(accountConfigPageReq.getPayeeType());
        processQueryDate(accountConfigPageReq,accountConfigPageQueryDto);
        PageResult<MerchantPaymentAccountConfigDO> page = merchantConfigService.queryConfigPage(accountConfigPageQueryDto);
        List<MerchantPaymentAccountConfigDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<MerchantReportPaymentConfigResp> merchantReportInfoListResps = BeanUtil.copyToList(list, MerchantReportPaymentConfigResp.class);
        return buildPageResult(page, merchantReportInfoListResps);
    }
    @Operation(summary = "汇电通白名单配置不分页查询")
    @GetMapping("/payment/account/list")
    CommonResult<List<MerchantReportPaymentConfigResp>> merchantPaymentConfigList(MerchantPaymentAccountConfigPageReq accountConfigPageReq) {
        MerchantPaymentAccountConfigPageQueryDto accountConfigPageQueryDto = new MerchantPaymentAccountConfigPageQueryDto();
        accountConfigPageQueryDto.setLikeName(accountConfigPageReq.getName());
        accountConfigPageQueryDto.setLikeCardNo(accountConfigPageReq.getCardNo());
        accountConfigPageQueryDto.setAccountType(accountConfigPageReq.getAccountType());
        accountConfigPageQueryDto.setEntryType(accountConfigPageReq.getEntryType());
        accountConfigPageQueryDto.setPayeeType(accountConfigPageReq.getPayeeType());
        List<MerchantPaymentAccountConfigDO> list = merchantConfigService.queryConfigList(accountConfigPageQueryDto);
        List<MerchantReportPaymentConfigResp> merchantReportInfoListResps = BeanUtil.copyToList(list, MerchantReportPaymentConfigResp.class);
        return CommonResult.success(merchantReportInfoListResps);
    }

    @Operation(summary = "新增汇电通白名单配置")
    @PostMapping("/payment/account")
    CommonResult<Void> addMerchantPaymentAccountConfig(@Validated @RequestBody MerchantPaymentAccountConfigReq merchantReportConfigPageReq) {
        return merchantConfigAppService.addMerchantPaymentAccountConfig(merchantReportConfigPageReq);
    }

    @Operation(summary = "更新汇电通白名单配置")
    @PutMapping("/payment/account")
    CommonResult<Void> updateMerchantPaymentAccountConfig(@Validated @RequestBody MerchantPaymentAccountConfigReq merchantReportConfigPageReq) {
        return merchantConfigAppService.updateMerchantPaymentAccountConfig(merchantReportConfigPageReq);
    }

    @Operation(summary = "删除汇电通白名单配置")
    @DeleteMapping("/payment/account")
    CommonResult<Void> delMerchantPaymentAccountConfig(@Validated @RequestBody DeleteCodeReq deleteIdReq) {
        return merchantConfigAppService.delMerchantPaymentAccountConfig(deleteIdReq.getCode());
    }

    @Operation(summary = "汇电通分账关系查询")
    @GetMapping("/payment/mapping")
    CommonResult<PageResult<MerchantReportPaymentConfigResp>> merchantReportConfigPage(MerchantPaymentConfigMappingPageReq mappingPageReq) {
        MerchantPaymentConfigMappingPageQueryDto merchantPaymentAccountConfigPageQueryDto = BeanUtil.copyProperties(mappingPageReq, MerchantPaymentConfigMappingPageQueryDto.class);
        PageResult<MerchantPaymentConfigMappingDto> page = merchantConfigService.queryConfigMappingPage(merchantPaymentAccountConfigPageQueryDto);
        List<MerchantPaymentConfigMappingDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<MerchantReportPaymentConfigResp> merchantReportInfoListResps = BeanUtil.copyToList(list, MerchantReportPaymentConfigResp.class);
        return buildPageResult(page, merchantReportInfoListResps);
    }

    @Operation(summary = "汇电通分账关系配置")
    @PostMapping("/payment/mapping")
    CommonResult<Void> saveMerchantConfigMapping(@Validated @RequestBody MerchantPaymentConfigMappingReq merchantReportConfigPageReq) {
        return merchantConfigAppService.saveMerchantConfigMapping(merchantReportConfigPageReq);
    }

    @Operation(summary = "汇电通删除分账关系配置")
    @DeleteMapping("/payment/mapping")
    CommonResult<Void> delMerchantConfigMapping(@Validated @RequestBody MerchantPaymentConfigMappingReq merchantReportConfigPageReq) {
        return merchantConfigAppService.delMerchantConfigMapping(merchantReportConfigPageReq);
    }

    @Operation(summary = "保存汇电通配置")
    @PostMapping("/limit")
    CommonResult<Void> limitConfig(@Validated @RequestBody MerchantLimitConfigReq merchantLimitConfigReq) {
        return merchantConfigAppService.limitConfig(merchantLimitConfigReq);
    }

    @Operation(summary = "查询汇电通配置")
    @GetMapping("/limit")
    CommonResult<MerchantLimitPaymentDto> queryLimitConfig(@RequestParam(value = "sn") String sn) {
        return CommonResult.success(merchantConfigAppService.queryLimitConfig(sn));
    }

}
